$(document).ready(function () {


    initSize();

})
//登录验证
$("#login-submit").click(function () {
    if ($("#username").val() == '') {
        $('.submit').find('span').html('请输入用户名');
    } else if ($("#password").val() == '') {
        $('.submit').find('span').html('请输入密码');
    } else {
        login();
    }

});
//回车确认事件
document.onkeydown = function (event) {
    if (event.keyCode == 13) {
        $("#login-submit").trigger("click");
    }
}


/*初始化默认字体大小*/
function initSize() {
    let fontSize;
    resize();
    window.onresize = resize;

    function resize() {

        let winW = window.innerWidth;
        if (winW >= 1440) { fontSize = winW / 1440 * 100 }
        document.getElementsByTagName('html')[0].style.cssText = "font-size:" + fontSize + "px";
    }

}
function login() {
    $('.submit').find('span').html('');
    var accout = $("#username").val();
    var password = $("#password").val();
    var url = '/rest/v1/login';
    $.ajax({
        url: url,
        type: "POST",
        contentType: "application/json",
        data: JSON.stringify({
            "username": accout,
            "password": password,
            "type": 2
        }),
        success: function (data) {

            if (data.status == 200 && data.result.sessionId) {
                let log = {};
                if (data.result.userProjectRole[0] != undefined) {
                    log = data.result.userProjectRole[0];
                }
                var roleid = log.roleId;
                var uuid = data.result.uuid;

                log.cellphone = data.result.cellphone;
                log.idNumber = data.result.idNumber;
                localStorage.setItem('siteList', JSON.stringify(data.result.userProjectRole));
                localStorage.setItem('sessionId', data.result.sessionId);
                localStorage.setItem('username', data.result.realname);
                localStorage.setItem('loginUser', JSON.stringify(log));
				localStorage.setItem('isLogin', JSON.stringify(data.result));
				
				if (roleid == 1 || roleid == 2 || roleid == 3) {
					window.location.href = "index.html";
				} else {
					window.location.href = "home.html";
				}
				
                // if (data.result.uuid == null || data.result.uuid == '') {
                //     if (roleid == 1 || roleid == 2 || roleid == 3) {
                //         window.location.href = "index.html";
                //     } else {
                //         window.location.href = "home.html";
                //     }
                // } else {
                //     $.ajax({
                //         url: '/rest/v1/account/sso?uuid=' + data.result.uuid,
                //         type: "get",
                //         contentType: "application/json",
                //         data: JSON.stringify({
                //             "uuid": data.result.uuid,
                //         }),
                //         success: function (data) {
                //             localStorage.setItem('isLogin', JSON.stringify(data.result));

                //             if (window.location.href.indexOf('ics') > -1) {
                //                 window.location.href = 'http://www.njdtwise.com/FirePM/html/sitelogin.html?uuid=' + uuid + '&roleid=' + roleid;
                //             } else if (window.location.href.indexOf('dev') > -1) {
                //                 window.location.href = 'http://dev.njdtwise.com/FirePM/html/sitelogin.html?uuid=' + uuid + '&roleid=' + roleid;
                //             } else {
                //                 window.location.href = '../../../FirePM/html/sitelogin.html?uuid=' + uuid + '&roleid=' + roleid;
                //             }
                //         },
                //         error: function (result) {

                //         }
                //     });
                // }



            } else {
                $('.submit').find('span').html('用户名或密码错误');
            }
        },
        error: function (result) {

        }
    });
}